//
//  CodeSettingCell.m
//  SIPphone
//
//  Created by wrt on 14-2-25.
//  Copyright (c) 2014年 wrtsoft. All rights reserved.
//

#import "CodeSettingCell.h"

@implementation CodeSettingCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)switchOnOrOff:(id)sender
{
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    NSLog(@"%lu",self.codeNumber);
    if(self.codeSwitch.on)  //开着的
    {
        [interface setEnabledAudioAndVideo:self.codeNumber andOn:1];
        NSLog(@"on");
        if ([self.codetType isEqualToString:@"video"])
        {
            NSString *tempCodeName = [interface getAudioAndVideoName:self.codeNumber];
            NSMutableDictionary *tempDir = [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath:@"videoCode.plist"]];
            NSArray *tempKeyArray = [[NSArray alloc] init];
            tempKeyArray = [tempDir allKeys];
            for (NSString *str in tempKeyArray)
            {
                if ([str isEqualToString:tempCodeName])
                {
                    [tempDir setObject:[NSNumber numberWithInt:1] forKey:str];
                    [tempDir writeToFile:[self dataFilePath:@"videoCode.plist"] atomically:YES];
                    break;
                }
            }
        }
        if ([self.codetType isEqualToString:@"audio"])
        {
            NSString *tempCodeName = [interface getAudioAndVideoName:self.codeNumber];
            NSMutableDictionary *tempDir = [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath:@"audioCode.plist"]];
            NSArray *tempKeyArray = [[NSArray alloc] init];
            tempKeyArray = [tempDir allKeys];
            for (NSString *str in tempKeyArray)
            {
                if ([str isEqualToString:tempCodeName])
                {
                    [tempDir setObject:[NSNumber numberWithInt:1] forKey:str];
                    [tempDir writeToFile:[self dataFilePath:@"audioCode.plist"] atomically:YES];
                    break;
                }
            }
        }
    }
    else    //关着的
    {
        [interface setEnabledAudioAndVideo:self.codeNumber andOn:0];
        NSLog(@"off");
        if ([self.codetType isEqualToString:@"video"])
        {
            NSString *tempCodeName = [interface getAudioAndVideoName:self.codeNumber];
            NSMutableDictionary *tempDir = [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath:@"videoCode.plist"]];
            NSArray *tempKeyArray = [[NSArray alloc] init];
            tempKeyArray = [tempDir allKeys];
            for (NSString *str in tempKeyArray)
            {
                if ([str isEqualToString:tempCodeName])
                {
                    [tempDir setObject:[NSNumber numberWithInt:0] forKey:str];
                    [tempDir writeToFile:[self dataFilePath:@"videoCode.plist"] atomically:YES];
                    break;
                }
            }
        }
        if ([self.codetType isEqualToString:@"audio"])
        {
            NSString *tempCodeName = [interface getAudioAndVideoName:self.codeNumber];
            NSMutableDictionary *tempDir = [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath:@"audioCode.plist"]];
            NSArray *tempKeyArray = [[NSArray alloc] init];
            tempKeyArray = [tempDir allKeys];
            for (NSString *str in tempKeyArray)
            {
                if ([str isEqualToString:tempCodeName])
                {
                    [tempDir setObject:[NSNumber numberWithInt:0] forKey:str];
                    [tempDir writeToFile:[self dataFilePath:@"audioCode.plist"] atomically:YES];
                    break;
                }
            }
        }
    }
}

//文件路径  videoCode.plist 和 audioCode.plist
- (NSString *)dataFilePath:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:fileName];
}

@end
